#include "cmaterial.h"

#include <QDomDocument>
#include <QFile>


CMaterial::CMaterial()
{
    this->m_fRed = 0.0;
    this->m_fGreen = 0.0;
    this->m_fBlue = 0.0;
    this->m_fReflection = 0.0;
    this->m_fRefraction = 0.0;
    this->m_bTransparent = false;
    this->m_fN = 1.0;
    this->m_fNred = 1.0;
    this->m_fNgreen = 1.0;
    this->m_fNblue = 1.0;
    this->m_fRedS = 1.0;
    this->m_fGreenS = 1.0;
    this->m_fBlueS = 1.0;
    this->m_fPowerS = 60.0;
}

CMaterial::CMaterial(float r, float g, float b, float reflection, float rS, float gS, float bS, float PowerS, bool bTrans, float n,  float refrac)
{
    this->m_fRed = r;
    this->m_fGreen = g;
    this->m_fBlue = b;
    this->m_fReflection = reflection;
    this->m_fRefraction = refrac;
    this->m_bTransparent = bTrans;
    this->m_fN = n;
    this->m_fNred = n;
    this->m_fNgreen = n;
    this->m_fNblue = n;
    this->m_fRedS = rS;
    this->m_fGreenS = gS;
    this->m_fBlueS = bS;
    this->m_fPowerS = PowerS;
}

CMaterial::CMaterial(const CMaterial & orig)
{
    this->m_fRed = orig.m_fRed;
    this->m_fGreen = orig.m_fGreen;
    this->m_fBlue = orig.m_fBlue;
    this->m_fReflection = orig.m_fReflection;
    this->m_fRefraction = orig.m_fRefraction;
    this->m_bTransparent = orig.m_bTransparent;
    this->m_fN = orig.m_fN;
    this->m_fNred = orig.m_fNred;
    this->m_fNgreen = orig.m_fNgreen;
    this->m_fNblue = orig.m_fNblue;
    this->m_fRedS = orig.m_fRedS;
    this->m_fGreenS = orig.m_fGreenS;
    this->m_fBlueS = orig.m_fBlueS;
    this->m_fPowerS = orig.m_fPowerS;
}

bool CMaterial::loadMaterial(const QString sFileName)
{
    QFile file(sFileName);
    if (!file.open(QFile::ReadOnly | QFile::Text))
        return false;
    QDomDocument doc;
    if (!doc.setContent(&file, false))
    {
        file.close();
        return false;
    }
    file.close();
    QDomElement root = doc.documentElement();
    root = root.firstChildElement();
    while(!root.isNull())
    {
        if (root.tagName() == "color")
        {
            QDomElement clrElem = root.firstChildElement();
            while (!clrElem.isNull())
            {
                if (clrElem.tagName() == "red")
                    this->m_fRed = clrElem.text().toDouble();
                else if (clrElem.tagName() == "green")
                    this->m_fGreen = clrElem.text().toDouble();
                else if (clrElem.tagName() == "blue")
                    this->m_fBlue = clrElem.text().toDouble();
                clrElem = clrElem.nextSiblingElement();
            }
        }
        else if (root.tagName() == "reflection")
            this->m_fReflection = root.text().toDouble();
        root = root.nextSiblingElement();
    }
    return false;
}

CMaterial * CMaterial::getvoidContext()
{
    return new CMaterial(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, true, 1.0, 1.0);
}

CMaterial * CMaterial::getairContext()
{
    return new CMaterial(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, true, 1.0003, 1.0003);
}

void CMaterial::setTransparent(bool bTrans, float N, float refraction)
{
    this->m_bTransparent = bTrans;
    this->m_fN = N;
    this->m_fNred = N;
    this->m_fNgreen = N;
    this->m_fNblue = N;
    this->m_fRefraction = refraction;
}

void CMaterial::setRefractionInd(float nR, float nG, float nB)
{
    if (nR < 1.0)
    {
        this->m_fN = 1.0;
        this->m_fNred = 1.0;
        this->m_fNgreen = 1.0;
        this->m_fNblue = 1.0;
    }
    else
    {
        this->m_fN = nR;
        this->m_fNred = nR;
        if (nG < 1.0 || nB < 1.0)
        {
            this->m_fNgreen = nR;
            this->m_fNblue = nB;
        }
        else
        {
            this->m_fNgreen = nG;
            this->m_fNblue = nB;
        }
    }
}
